home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4745 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.4 KB

  1. Path: citr.uq.oz.au!usenet
  2. From: Martin Pool <martin>
  3. Newsgroups: comp.lang.c++
  4. Subject: 'return' keyword in G++ (was Re: Parameterless functions)
  5. Date: 31 Jan 1996 23:22:35 GMT
  6. Organization: CiTR Pty Ltd
  7. Message-ID: <4eotjr$p9c@joppa.citr.uq.oz.au>
  8. References: <9601191115.aa06760@paris.ics.uci.edu> <4dpi25$vp@colossus.holonet.net> <4e7pqo$e4l@tech.cftnet.com>
  9. NNTP-Posting-Host: cooloola.citr.uq.oz.au
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3_U1 sun4m)
  14. X-URL: news:4e7pqo$e4l@tech.cftnet.com
  15.  
  16. wcowley@cftnet.com (Wes Cowley) wrote:
  17. >Russell Blackadar (russell@news.mdli.com) wrote:
  18. >: Raymond Klefstad, Ph.D. (klefstad@catalina.ICS.UCI.EDU) wrote:
  19. >: 
  20. >: : Also, I noticed the following in some of the standard headers for gcc:
  21. >: : int foo(int x) return y
  22. >: 
  23. >: It's not C++, and I wouldn't want my compiler to accept code like this.
  24. >: Are you sure this line actually compiles, or is it just a comment?
  25. >
  26. >If I recall correctly from the readmes, it's a g++ extension.
  27.  
  28. It is in fact a g++ extension, and a useful one.  The best explanation is an
  29. excerpt from the info manual (gcc 2.6.3).
  30.  
  31. Martin
  32.  
  33. -- info page begins --
  34.  
  35. Named Return Values in C++
  36. ==========================
  37.  
  38.    GNU C++ extends the function-definition syntax to allow you to
  39. specify a name for the result of a function outside the body of the
  40. definition, in C++ programs:
  41.  
  42.      TYPE
  43.      FUNCTIONNAME (ARGS) return RESULTNAME;
  44.      {
  45.        ...
  46.        BODY
  47.        ...
  48.      }
  49.  
  50.    You can use this feature to avoid an extra constructor call when a
  51. function result has a class type.  For example, consider a function
  52. `m', declared as `X v = m ();', whose result is of class `X':
  53.  
  54.      X
  55.      m ()
  56.      {
  57.        X b;
  58.        b.a = 23;
  59.        return b;
  60.      }
  61.  
  62.    Although `m' appears to have no arguments, in fact it has one
  63. implicit argument: the address of the return value.  At invocation, the
  64. address of enough space to hold `v' is sent in as the implicit argument.
  65. Then `b' is constructed and its `a' field is set to the value 23.
  66. Finally, a copy constructor (a constructor of the form `X(X&)') is
  67. applied to `b', with the (implicit) return value location as the
  68. target, so that `v' is now bound to the return value.
  69.  
  70.    But this is wasteful.  The local `b' is declared just to hold
  71. something that will be copied right out.  While a compiler that
  72. combined an "elision" algorithm with interprocedural data flow analysis
  73. could conceivably eliminate all of this, it is much more practical to
  74. allow you to assist the compiler in generating efficient code by
  75. manipulating the return value explicitly, thus avoiding the local
  76. variable and copy constructor altogether.
  77.  
  78.    Using the extended GNU C++ function-definition syntax, you can avoid
  79. the temporary allocation and copying by naming `r' as your return value
  80. as the outset, and assigning to its `a' field directly:
  81.  
  82.      X
  83.      m () return r;
  84.      {
  85.        r.a = 23;
  86.      }
  87.  
  88. The declaration of `r' is a standard, proper declaration, whose effects
  89. are executed *before* any of the body of `m'.
  90.  
  91.    Functions of this type impose no additional restrictions; in
  92. particular, you can execute `return' statements, or return implicitly by
  93. reaching the end of the function body ("falling off the edge").  Cases
  94. like
  95.  
  96.      X
  97.      m () return r (23);
  98.      {
  99.        return;
  100.      }
  101.  
  102. (or even `X m () return r (23); { }') are unambiguous, since the return
  103. value `r' has been initialized in either case.  The following code may
  104. be hard to read, but also works predictably:
  105.  
  106.      X
  107.      m () return r;
  108.      {
  109.        X b;
  110.        return b;
  111.      }
  112.  
  113.    The return value slot denoted by `r' is initialized at the outset,
  114. but the statement `return b;' overrides this value.  The compiler deals
  115. with this by destroying `r' (calling the destructor if there is one, or
  116. doing nothing if there is not), and then reinitializing `r' with `b'.
  117.  
  118.    This extension is provided primarily to help people who use
  119. overloaded operators, where there is a great need to control not just
  120. the arguments, but the return values of functions.  For classes where
  121. the copy constructor incurs a heavy performance penalty (especially in
  122. the common case where there is a quick default constructor), this is a
  123. major savings.  The disadvantage of this extension is that you do not
  124. control when the default constructor for the return value is called: it
  125. is always called at the beginning.
  126.  
  127.